package com.mhs.pb.api.impl;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import com.mhs.pb.api.PBService;
import com.mhs.pb.api.model.PBAccount;
import com.mhs.pb.api.model.PBBasicInfo;
import com.mhs.pb.api.model.PBRecord;
import com.mhs.pb.api.model.PBRelation;
import com.mhs.pb.api.model.PBResource;
import com.mhs.pb.api.service.IAccountActivityManager;
import com.mhs.pb.api.service.impl.AccountActivityManager;
import com.mhs.pb.domain.Account;
import com.mhs.pb.domain.Relation;
import com.mhs.pb.service.IAccountManager;
import com.mhs.pb.service.IRelationManager;
import com.mhs.pb.service.IServiceProxy;
import com.mhs.pb.service.impl.ServiceProxy;
import com.mhs.pb.utils.ModelConvetor;

public class PBServiceImpl implements PBService {
private IRelationManager relationManager;
private IAccountManager accountManager;
private IAccountActivityManager activityManager;

public PBServiceImpl() {
	IServiceProxy proxy = new ServiceProxy();
	relationManager = proxy.getProxiedRelationManager();
	accountManager = proxy.getProxiedAccountManager();
	activityManager = new AccountActivityManager();
	activityManager.run();
}

@Override
public PBAccount getAccount(HttpServletResponse response) {
	PBAccount account = new PBAccount();
	account.setAccount("mason.mei@mhs.com");
	account.setEmail("mason.mei@mhs.com");
	account.setId(1l);
	account.setCreatedate(new Timestamp(Calendar.getInstance().getTime().getTime()));
	PBRelation relation = new PBRelation();
	relation.setRelationtype("Family");
	PBBasicInfo basicinfo = new PBBasicInfo();
	relation.setBasicinfo(basicinfo);
	account.setRelation(relation);
	account.setUsername("mason mei");
	String token = "1111111111111111111111111111";
	response.addHeader("Token", token);
	return account;
}

@Override
public PBAccount postAccount(PBAccount account) {
	account = new PBAccount();
	account.setAccount("mason.mei@mhs.com");
	account.setEmail("mason.mei@mhs.com");
	account.setId(1l);
	account.setCreatedate(new Timestamp(Calendar.getInstance().getTime().getTime()));
	account.setRelation(new PBRelation());
	account.setUsername("mason mei");
	return account;
}

@Override
public PBAccount createAccount(PBAccount account, HttpServletResponse response) {
	PBAccount convertAccount = ModelConvetor.convertAccount(accountManager.createAccount(ModelConvetor.unConvertAccount(account)));
	String token = activityManager.putAccount(convertAccount);
	response.addHeader("Token", token);
	return convertAccount;
}

@Override
public PBAccount validateAccount(PBAccount account, HttpServletResponse response) {
	PBAccount convertAccount = ModelConvetor.convertAccount(accountManager.validateAccount(ModelConvetor.unConvertAccount(account)));
	String token = activityManager.putAccount(convertAccount);
	response.addHeader("Token", token);
	return convertAccount;
}

@Override
public PBAccount modifyAccount(PBAccount account, HttpServletResponse response) {
	PBAccount convertAccount = ModelConvetor.convertAccount(accountManager.modifyAccount(ModelConvetor.unConvertAccount(account)));
	String token = activityManager.putAccount(convertAccount);
	response.addHeader("Token", token);
	return convertAccount;
}

@Override
public Set<PBRelation> listRelations(String token) {
	PBAccount account = activityManager.getAccount(token);
	if (account != null) {
		Account acc = ModelConvetor.unConvertAccount(account);
		return ModelConvetor.convertRelations(relationManager.listAllRelations(acc));
	}
	return null;
}

@Override
public boolean addRelation(PBRelation relation, String token) {
	try {
		PBAccount account = activityManager.getAccount(token);
		if (account != null) {
			Relation unConvertRelation = ModelConvetor.unConvertRelation(relation, account);
			relationManager.addRelation(unConvertRelation);
			return true;
		}
		return false;
	} catch (Exception e) {
		return false;
	}
}

@Override
public boolean modifyRelation(PBRelation relation, String token) {
	try {
		PBAccount account = activityManager.getAccount(token);
		if (account != null) {
			relationManager.modifyRelation(ModelConvetor.unConvertRelation(relation, account));
			return true;
		}
		return false;
	} catch (Exception e) {
		return false;
	}
}

@Override
public boolean deleteRelation(PBRelation relation, String token) {
	try {
		activityManager.updateAccountActivity(token);
		relationManager.deleteRelation(ModelConvetor.unConvertRelation(relation, null));
		return true;
	} catch (Exception e) {
		return false;
	}
}

@Override
public boolean deleteRecord(PBRecord record, String token) {
	try {
		activityManager.updateAccountActivity(token);
		relationManager.deleteRecord(ModelConvetor.unConvertRecord(record, null));
		return true;
	} catch (Exception e) {
		return false;
	}
}

@Override
public boolean deleteResource(PBResource resource, String token) {
	try {
		activityManager.updateAccountActivity(token);
		relationManager.deleteResource(ModelConvetor.unConvertResource(resource, null));
		return true;
	} catch (Exception e) {
		return false;
	}
}

@Override
public Set<PBRelation> getRelations(String type, String value, String token) {
	String lctype = type.toLowerCase().trim();
	PBAccount account = activityManager.getAccount(token);
	if (account != null) {
		Account acc = ModelConvetor.unConvertAccount(account);
		if (lctype.equals("name")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByName(acc, value));
		} else if (lctype.equals("cellphone")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByCellPhone(acc, value));
		} else if (lctype.equals("phone")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByPhone(acc, value));
		} else if (lctype.equals("contact")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByContact(acc, value));
		} else if (lctype.equals("accomplishment")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByAccomplishment(acc, value));
		} else if (lctype.equals("recorddesc")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByPersonalRecordDes(acc, value));
		} else if (lctype.equals("resourcedesc")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByResourceDes(acc, value));
		}
	}
	return null;
}

@Override
public Set<PBRelation> getRelations(String type, String title, String value, String token) {
	String lctype = type.toLowerCase().trim();
	PBAccount account = activityManager.getAccount(token);
	if (account != null) {
		if (lctype.equals("accomplishment")) {
			return ModelConvetor.convertRelations(relationManager.getRelationsByTypeAndAccomplishment(ModelConvetor.unConvertAccount(account), title, value));
		} else if (lctype.equals("recorddesc")) {
			return ModelConvetor
			        .convertRelations(relationManager.getRelationsByTypeAndPersonalRecordDes(ModelConvetor.unConvertAccount(account), title, value));
		}
	}
	return null;
}

}
